

import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;

public class FilledTriangleTool extends Tool {

  protected Point startingMousePosition;
  protected Point currentMousePosition, thirdPoint;
  protected Color saveColor;

  public FilledTriangleTool(DrawingCanvas canvas, LinkedList<ShapeObject> ShapeObjects, MDUser mdUser) {
	  super(canvas, ShapeObjects, mdUser);
  }

  
  protected void drawTriangle(int[] x, int[] y){
    Graphics iBGraphics = canvas.getImageBufferGraphics();
    iBGraphics.fillPolygon(new Polygon(x, y, 3));
    canvas.repaint();
  }


 public void mousePressed(MouseEvent e)  {
   /* Establish starting point for next drawing */
	 thirdPoint = new Point();
   startingMousePosition = e.getPoint();
   currentMousePosition = startingMousePosition;
   Graphics iBGraphics = canvas.getImageBufferGraphics();
   saveColor = iBGraphics.getColor( );
   iBGraphics.setXORMode(Color.lightGray);
   iBGraphics.setColor(Color.white);
   int[] x = {startingMousePosition.x, startingMousePosition.x, startingMousePosition.x};
   int[] y = {startingMousePosition.y, startingMousePosition.y, startingMousePosition.y};
   drawTriangle(x, y);
 }

 public void mouseDragged(MouseEvent e)  {
   Point newMousePosition = e.getPoint();
   int dx = currentMousePosition.x - startingMousePosition.x;
   thirdPoint.x = startingMousePosition.x - dx;
   thirdPoint.y = currentMousePosition.y;
   int[] x = {startingMousePosition.x, currentMousePosition.x, thirdPoint.x};
   int[] y = {startingMousePosition.y, currentMousePosition.y, thirdPoint.y};
   drawTriangle(x, y);
   dx = newMousePosition.x - startingMousePosition.x;
   thirdPoint.x = startingMousePosition.x - dx;
   thirdPoint.y = newMousePosition.y;
   int[] x1 = {startingMousePosition.x, newMousePosition.x, thirdPoint.x};
   int[] y1 = {startingMousePosition.y, newMousePosition.y, thirdPoint.y};
   drawTriangle(x1, y1);
   currentMousePosition = newMousePosition;
 }


  public void mouseReleased(MouseEvent e) { 
	  	Point newMousePosition = e.getPoint();
		Point thirdPoint = new Point();
	   int dx = newMousePosition.x - startingMousePosition.x;
	   thirdPoint.x = startingMousePosition.x - dx;
//	   int dy = newMousePosition.y - startingMousePosition.y;
	   thirdPoint.y = newMousePosition.y;
	   int[] x = {startingMousePosition.x, newMousePosition.x, thirdPoint.x};
	   int[] y = {startingMousePosition.y, newMousePosition.y, thirdPoint.y};
		drawTriangle(x, y);
	    Graphics iBGraphics = canvas.getImageBufferGraphics();
	    int minX = Math.min(Math.min(startingMousePosition.x, newMousePosition.x), thirdPoint.x);
		int minY = Math.min(Math.min(startingMousePosition.y, newMousePosition.y), thirdPoint.y);
		int maxX = Math.max(Math.max(startingMousePosition.x, newMousePosition.x), thirdPoint.x);
		int maxY = Math.max(Math.max(startingMousePosition.y, newMousePosition.y), thirdPoint.y);
		boolean invertedTriangle = false;
		System.out.println("minY:" +minY + " st.x:" + startingMousePosition.x);
		if(minY != startingMousePosition.y)
			invertedTriangle = true;
	    ShapeObjects.add(new FilledTriangleObject(new Point(minX, minY), new Point(maxX, maxY), saveColor, invertedTriangle));	    
		iBGraphics.setPaintMode();
		paintObjects(0);
		iBGraphics.setColor(saveColor);
  }

  public void keyPressed(KeyEvent e) {}

}
